import json
import requests as rq
import time

session = rq.session()
loginUrl = "https://xxcapp.xidian.edu.cn/uc/wap/login/check"
indexUrl = "https://xxcapp.xidian.edu.cn/site/ncov/xidiandailyup"
submitUrl = "https://xxcapp.xidian.edu.cn/xisuncov/wap/open-report/save"
notifyUrl = "" # 替换为自己的server酱链接

userdata = {
    "username": "",
    "password": ""
}
submitData = {
    "address": "陕西省西安市长安区兴隆街道西安电子科技大学长安校区A区家属楼4号楼",
    "area": "陕西省 西安市 长安区",
    "askforleave": "0",
    "city": "西安市",
    "geo_api_info": '{"type":"complete","position":{"Q":34.121760253907,"R":108.829217936198,"lng":108.829218,"lat":34.12176},"location_type":"html5","message":"Get ipLocation failed.Get geolocation success.Convert Success.Get address success.","accuracy":80,"isConverted":true,"status":1,"addressComponent":{"citycode":"029","adcode":"610116","businessAreas":[],"neighborhoodType":"","neighborhood":"","building":"","buildingType":"","street":"雷甘路","streetNumber":"264号","country":"中国","province":"陕西省","city":"西安市","district":"长安区","township":"兴隆街道"},"formattedAddress":"陕西省西安市长安区兴隆街道西安电子科技大学长安校区A区家属楼4号楼","roads":[],"crosses":[],"pois":[],"info":"SUCCESS"}',
    "number": "",
    "province": "陕西省",
    "qtqk": "",
    "realname": "",
    "sfcyglq": 0,
    "sfyzz": 0,
    "sfzx": 1,
    "tw": "1",
    "ymtys": "0"
}

def submit(result,userdata,submitData):
    session = rq.session()
    response = session.post(loginUrl, data=userdata)
    if response.status_code == 200:
        response = session.get(indexUrl)
        response = session.post(submitUrl, data=submitData)
        # 标志位 此处是针对返回值做的处理
        returnCode = response.json().get("e")
        if (returnCode in [0,1] or returnCode in ['0','1']):
            result.append(0)
        else:
            result.append(1)
    else: 
        time.sleep(3)
        submit(result,userdata,submitData)

def run(user,result):
    for i in range(len(user)):
        userdata["username"]=user[i][0]
        userdata["password"]=user[i][1]
        submitData["number"]=user[i][0]
        submitData["realname"]=user[i][2]
        submit(result,userdata,submitData)
    return result

def notify(result):
    string  = ""
    for i in range(len(result)):
        if result[i] == 1:
            string = ",".join([string,user[i][2]])
            
    if len(string) != 0:
        string = "".join([string,"签到出现问题！"])[1:]
    else:
        string = "".join([string,"签到成功！"])
    # print(string)
    notifydata = {
        "text" : string,
        "desp" : "晨午检"
    }
    rq.post(notifyUrl,data = notifydata)

if __name__ == "__main__":
    user = []
    result = []
    user.append(["学号","教务处密码","姓名"])
    notify(run(user,result))
